package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/roles")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    //查询所有角色
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有角色信息");
        List<Role> roleList = roleService.list(); // 获取所有 status=1 的角色
        return Result.success(roleList);
    }

    //修改角色数据
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色信息，参数：{}", role);
        roleService.update(role);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询角色，参数：{}", id);
        Role role = roleService.get(id);
        return Result.success(role);
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色，参数：{}", id);
        roleService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色信息，参数：{}", role);
        roleService.addRole(role);
        return Result.success();
    }

    @GetMapping
    public Result list(RoleDTO dto) {
        log.info("查询角色列表，参数：{}", dto);
        PageResult<Role> pageResult = roleService.list(dto);
        return Result.success(pageResult);
    }
}
